/**
 * @title  contract deploy
 */
const { devlopmentChains, chainConfig } = require("../helper-hardhat-config")
const { network, ethers } = require("hardhat")
const { verifyContract } = require("../utils/verify")
const fs = require("fs")

module.exports = async (hre) => {
    const { getNamedAccounts, deployments } = hre
    const { deploy, log } = deployments
    const { deployer } = await getNamedAccounts() //获取部署合约的地址

    log("the dynamicNFT contract deploying ")

    let priceFeeAddress

    if (devlopmentChains.includes(network.name)) {
        priceFeeAddress = (await ethers.getContract("MockV3Aggregator")).address
    } else {
        priceFeeAddress = chainConfig[network.config.chainId].priceFee
    }

    const lowSVG = await fs.readFileSync("./images/dynamicNFT/sad.svg", {
        encoding: "utf8",
    })
    const highSVG = await fs.readFileSync("./images/dynamicNFT/happy.svg", {
        encoding: "utf8",
    })

    const args = [priceFeeAddress, lowSVG, highSVG, 1]
    const dynamicNFT = await deploy("DynamicSvgNFT", {
        from: deployer,
        args: args,
        log: true,
        // @ts-ignore
        waitConfirmations: network.config.blockConfirmations || 1,
    })
    //校验合约代码
    if (
        !devlopmentChains.includes(network.name) &&
        process.env.ETHERSCAN_API_KEY
    ) {
        console.log("verifying contract")
        await verifyContract(dynamicNFT.address, args)
    }
}

module.exports.tags = ["all", "dynamicNFT"]
